package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruffian.library.widget.RTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.BillInfoListBean;

/**
 * Created By Dlx
 * on 2019/6/27
 */
public class ADA_BillItemList extends RecyclerView.Adapter<ADA_BillItemList.MyHolder> {

    private Context mContext;
    private List<BillInfoListBean> datas = new ArrayList<>();
    private int type; // 0  service   1 goods
    private BillOnClickListener listener;

    public ADA_BillItemList(Context context, int type) {
        mContext = context;
        this.type = type;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_bill_goods_list, null);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {

        final BillInfoListBean bean = datas.get(position);
        holder.tvName.setText(StringUtils.isEmpty(bean.getMemberPhone()) ? StringUtils.get(bean.getName(),"散客") :
                StringUtils.get(bean.getMemberName(),"暂无姓名"));
        holder.tvPhone.setText(StringUtils.getString(bean.getMemberPhone()));
        holder.billitemCheckoutTimeTv.setText(bean.getSettleTime());
        holder.billitemGoodsNumTv.setText(bean.getOrderCode());
        if ("1".equals(bean.getOrderType()) && "2".equals(bean.getOrderStatus()) && "0".equals(bean.getGoodsList().get(0).getIsTimerCard())) {
            holder.billitemReturnRtv.setVisibility(View.VISIBLE);
        } else {
            holder.billitemReturnRtv.setVisibility(View.GONE);
        }

        if ("3".equals(bean.getOrderType())) {
            holder.tvTotalPrice.setText(String.format("%s积分", BigDecimalUtil.getNumber(bean.getIntegralGoods())));
            holder.billitemPrintRtv.setVisibility(View.GONE);
        } else {
            holder.tvTotalPrice.setText(StringUtils.getFormattedMoneyWithUnit(bean.getTotalPrice()));
            holder.billitemPrintRtv.setVisibility(View.VISIBLE);
        }

        holder.goodsLayout.removeAllViews();

        for (int i = 0; i < 5; i++) {
//            if (type == 1) {
            if ("1".equals(bean.getOrderType()) || "3".equals(bean.getOrderType())) {//0服务 1商品
                holder.goodsLayout.addView(createGoodItem(position, i, "3".equals(bean.getOrderType())));
            } else {
                holder.goodsLayout.addView(createServiceItem(position, i));
            }
        }

        holder.cvRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCardClick(bean.getSaleId(), bean.getOrderType());
                } else {
                    ToastUtil.debugToast(mContext, "你忘记注册点击事件了", false);
                }
            }
        });
        holder.billitemReturnRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onReturnClick(bean);
                } else {
                    ToastUtil.debugToast(mContext, "你忘记注册点击事件了", false);
                }
            }
        });
        holder.billitemPrintRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onPrintClick(bean.getSaleId(), bean.getOrderType());
                } else {
                    ToastUtil.debugToast(mContext, "你忘记注册点击事件了", false);
                }
            }
        });

    }

    private View createServiceItem(int position, int i) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bill_goods_list_gooditem, null);
        TextView name = view.findViewById(R.id.goods_name_tv);
        TextView price = view.findViewById(R.id.goods_price_tv);

        if (i == 3) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
            params.weight = 1;
            params.height = 0;
            view.setLayoutParams(params);
            name.setText(" ");
            price.setText(" ");
            return view;
        }
        if (i == 4) {
            if (datas.get(position).getListOrder().size() > 1) {
                name.setText("……");
                price.setText("共" + datas.get(position).getListOrder().size() + "项");
            } else {
                name.setText(" ");
                price.setText(" ");
            }
            return view;
        }
        LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        vParams.bottomMargin = DisplayUtil.dip2px(mContext, 11);
        view.setLayoutParams(vParams);
        if (datas.get(position).getListOrder().size() > i) {
            name.setText(datas.get(position).getListOrder().get(i).getServiceName());
            price.setText(StringUtils.getFormattedMoneyWithUnit(datas.get(position).getListOrder().get(i).getPrice()));
        } else {
            name.setText(" ");
            price.setText(" ");
        }

        return view;
    }

    private View createGoodItem(int position, int i, boolean isIntegralGoods) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bill_goods_list_gooditem, null);
        TextView name = view.findViewById(R.id.goods_name_tv);
        TextView price = view.findViewById(R.id.goods_price_tv);

        if (i == 3) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
            params.weight = 1;
            params.height = 0;
            view.setLayoutParams(params);
            name.setText(" ");
            price.setText(" ");
            return view;
        }
        if (i == 4) {
            if (datas.get(position).getGoodsList().size() > 1) {
                name.setText("……");
                int goodsSubNum = 0;
                for (BillInfoListBean.GoodsListBean bean : datas.get(position).getGoodsList()) {
                    goodsSubNum += bean.getGoodsNum();
                }
                price.setText("共" + goodsSubNum + "项");
            } else {
                name.setText(" ");
                price.setText(" ");
            }
            return view;
        }

        LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        vParams.bottomMargin = DisplayUtil.dip2px(mContext, 11);
        view.setLayoutParams(vParams);
        if (datas.get(position).getGoodsList().size() > i) {
            name.setText(datas.get(position).getGoodsList().get(i).getGoodsName() +
                    "(" + datas.get(position).getGoodsList().get(i).getGoodsNum() + ")");
            //商品存在数量不为1的情况， 根据UE描述， 显示一个商品。价格显示单价X数量
//            price.setText("￥" + (datas.get(position).getGoodsList().get(i).getGoodsNum() == 1 ? datas.get(position).getGoodsList().get(i).getPrice() :
//                    Double.valueOf(datas.get(position).getGoodsList().get(i).getPrice()) * datas.get(position).getGoodsList().get(i).getGoodsNum()));
            //7-19 谢启繁矢口否认之前的设计，骗人的鬼，现修改为单价，之前逻辑保留，他要是再改，就给他看这个备注
            if (isIntegralGoods) {
                price.setText(String.format("%s积分", BigDecimalUtil.getNumber(datas.get(position).getGoodsList().get(i).getIntegral())));
            } else {
                price.setText(StringUtils.getFormattedMoneyWithUnit(datas.get(position).getGoodsList().get(i).getRealPay()));
            }
        } else {
            name.setText(" ");
            price.setText(" ");
        }
        return view;
    }

    public void setDatas(List<BillInfoListBean> data, boolean isClear) {
        if (isClear) {
            datas.clear();
        }
        if (data.size() != 0)
            datas.addAll(data);
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setClickListener(BillOnClickListener listener) {
        this.listener = listener;
    }

    public interface BillOnClickListener {
        void onCardClick(String saleId, String orderType);

        void onPrintClick(String saleId, String orderType);

        void onReturnClick(BillInfoListBean billInfoListBean);
    }


    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ic_portrait)
        AppCompatImageView icPortrait;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.billitem_checkout_time_tv)
        TextView billitemCheckoutTimeTv;
        @BindView(R.id.billitem_goods_num_tv)
        TextView billitemGoodsNumTv;
        @BindView(R.id.billitem_print_rtv)
        RTextView billitemPrintRtv;
        @BindView(R.id.billitem_return_rtv)
        RTextView billitemReturnRtv;
        @BindView(R.id.cv_root)
        CardView cvRoot;
        @BindView(R.id.goods_layout)
        LinearLayout goodsLayout;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
